package exam.axhu.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;



import exam.axhu.entity.Admin;
import exam.axhu.service.AdminService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class AdminLogin extends ActionSupport{
//defaultClass: adminLogin
	@Resource AdminService adminService;
	
	private Admin admin;
	
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	
	public String login(){
		if(admin.getAdminID()==null|| admin.getPassword()==null)
		{
			addActionError("用户名、密码、姓名、身份不能为空!");
			return "AdminLoginFailed";
		}
		
		admin=new Admin(admin.getAdminID(),admin.getPassword());
		if(adminService.findByID(admin.getAdminID())!=null)
		{
			if (admin.getPassword().equals(adminService.findByID(admin.getAdminID()).getPassword()))
			{
				ActionContext.getContext().getSession().put("adminInfo", adminService.findByID(admin.getAdminID()));
				return "AdminLoginSucceed";
			}
			else
			{
				addActionMessage("密码错误!请重新输入密码");
				return "AdminLoginFailed";
			}
		}
		addActionError("不存在此用户!");
		return "AdminLoginFailed";
	}
	
	
}
